<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <MessageSearch @search="reload" />
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="(columns as any)"
        :datasource="datasource"
        :scroll="{ x: 800 }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a-divider type="vertical" />
              <a @click="openEdit(record)">预览</a>
              <a-divider type="vertical" />
              <a-popconfirm
                title="确定要删除此公告数据吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 详情弹窗 -->
    <MessageEdit v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue';
  import type { EleProTable } from 'ele-admin-pro';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table';
  import { toDateString } from 'ele-admin-pro';
  import { pageMesg, removeMesg } from '@/api/neirong/message';
  import type { Notice, NoticeParam } from '@/api/neirong/notice/model';
  import MessageSearch from '@/views/neirong/message/components/message-search.vue';
  import MessageEdit from '@/views/neirong/message/components/message-edit.vue';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable>>();
  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      title: '编号',
      dataIndex: 'id',
      width:50
    },
    {
      title: '内容',
      width: 400,
      ellipsis: true,
      dataIndex: 'message'
    },
    {
      title: '留言者',
      dataIndex: 'name'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      align: 'center',
      hideInSetting: true
    }
  ]);

  // 表格选中数据
  const selection = ref<Notice[]>([]);

  // 当前编辑数据
  const current = ref<Notice>();

  // 是否显示预览弹窗
  const showEdit = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    return pageMesg({ ...where, ...orders, page, limit });
  };

  /* 搜索 */
  const reload = (where?: NoticeParam) => {
    selection.value = [];
    tableRef?.value?.reload({ page: 1, where: where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Notice) => {
    current.value = row;
    showEdit.value = true;
  };

  /* 删除单个 */
  const remove = (row: Notice) => {
    const hide = message.loading('请求中..', 0);
    removeMesg(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };
</script>
